package org.gangcai.mac.shop.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.bean.CommonResonseBean;
import org.gangcai.mac.shop.bean.EventBusBaseBean1;
import org.gangcai.mac.shop.bean.PullAuthoBean;
import org.gangcai.mac.shop.constants.Constant;
import org.gangcai.mac.shop.oberver.CommonObserver;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AuthoOperationActivity extends BaseAuthoActivity {

    @BindView(R.id.operator)
    TextView operator;
    @BindView(R.id.mobile)
    EditText mobile;
    @BindView(R.id.service_pwd)
    EditText servicePwd;
    @BindView(R.id.nextStep)
    Button nextStep;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        getAuthoData("getoperator").subscribe(new CommonObserver<PullAuthoBean>() {
            @Override
            public void onSuccess(PullAuthoBean value) {
                servicePwd.setText(value.getInfo().getService_pwd());
                mobile.setText(value.getInfo().getMobile());
                operator.setText(value.getInfo().getOperator());
            }
        });
    }

    @Override
    protected String getTitleName() {
        return "运营商";
    }

    @Override
    public int getcontentView() {
        return R.layout.activity_autho_operation;
    }

    @OnClick({R.id.nextStep, R.id.operator})
    public void onViewClicked(View v) {
        if (v.getId() == R.id.nextStep) {

            if (TextUtils.isEmpty(operator.getText().toString())) {
                ToastUtils.showShort("请填写运营商信息");
            } else if (!RegexUtils.isMobileExact(mobile.getText().toString())) {
                ToastUtils.showShort("请准确填写您的手机号码");
            } else if (TextUtils.isEmpty(servicePwd.getText().toString()) || servicePwd.getText().toString().length() != 6) {
                ToastUtils.showShort("请准确填写您的服务密码");
            } else {
                submitData();
            }
        } else if (v.getId() == R.id.operator) {
            final String[] items = {"中国移动", "中国联通", "中国电信"};
            new AlertDialog.Builder(this).
                    setTitle("请选择运营商").
                    setItems(items, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            operator.setText(items[which]);
                            dialog.dismiss();
                        }
                    }).show();
        }
    }

    private void submitData() {
        prompDialog.showLoading("请等待");
        HashMap<String, String> paramMap = new HashMap<>();
        paramMap.put("uid", SPUtils.getInstance().getString("uid"));
        paramMap.put("operator", operator.getText().toString());
        paramMap.put("mobile", mobile.getText().toString());
        paramMap.put("service_pwd", servicePwd.getText().toString());
        submitDataToServer("operator_ident", paramMap, null).
                subscribe(new CommonObserver<CommonResonseBean>(prompDialog) {
                    @Override
                    public void onSuccess(CommonResonseBean value) {
                        if (value.getCode() == Constant.CODE_SUCC) {
                            showMsg();
                            EventBus.getDefault().postSticky(new EventBusBaseBean1());
                            finish();
                        }
                    }
                });
    }
}
